namespace SpaceMutinyNameGenerator
{
	using System;
	using System.Xml;

	public sealed class NameConfigReader
	{
		private const String ConfigFile = "NameConfig.xml";
		private const String FirstNames = "firstnames";
		private const String MiddleNames = "middlenames";
		private const String LastNamesPrefix = "lastnameprefix";
		private const String LastNamesSuffix = "lastnamesuffix";

		private readonly XmlDocument _config;
		private readonly NameList _firstNames;
		private readonly NameList _lastNamePrefixes;
		private readonly NameList _lastNameSuffixes;
		private readonly NameList _middleNames;

		public NameConfigReader()
		{
			_firstNames = new NameList();
			_middleNames = new NameList();
			_lastNamePrefixes = new NameList();
			_lastNameSuffixes = new NameList();

			_config = new XmlDocument();
			_config.Load(ConfigFile);

			FillNameList(ref _firstNames, FirstNames);
			FillNameList(ref _middleNames, MiddleNames);
			FillNameList(ref _lastNamePrefixes, LastNamesPrefix);
			FillNameList(ref _lastNameSuffixes, LastNamesSuffix);
		}

		private void FillNameList(ref NameList lst, String xmlnode)
		{
			XmlNode namesNode = _config.DocumentElement.FirstChild;
			if (!namesNode.HasChildNodes)
			{
				return;
			}
			XmlNode names = namesNode.SelectSingleNode(xmlnode);
			foreach (XmlNode child in names.ChildNodes)
			{
				// filter out dupes
				if (!lst.Contains(child.InnerText))
				{
					lst.Add(child.InnerText);
				}
			}
		}

		internal NameList GetFirstNames()
		{
			return _firstNames;
		}

		internal NameList GetMiddleNames()
		{
			return _middleNames;
		}

		internal NameList GetLastNamePrefixes()
		{
			return _lastNamePrefixes;
		}

		internal NameList GetLastNameSuffixes()
		{
			return _lastNameSuffixes;
		}
	}
}